<?
    class Principio_Model_Company_AbstractMapper extends Principio_Mapper
    {
        protected static $db_table = null;
        protected static function getDbTableName() { return "Company"; }


        public static function delete(Principio_Model_Company_Abstract $model_company_abstract, $use_transaction = true)
        {
            try
            {
                if ($use_transaction)
                    self::getDBTable()->getAdapter()->beginTransaction();

                foreach ($model_company_abstract->getTypeInCompanyCollection() as $model_type_in_company)
                    $model_type_in_company->delete();


                foreach ($model_company_abstract->getAccountCollection() as $model_account)
                    $model_account->delete();

                self::getDBTable()->delete(['id=?' => $model_company_abstract->getID()]);

                if ($use_transaction)
                    self::getDBTable()->getAdapter()->commit();

                return true;
            }
            catch (Exception $ex)
            {
                self::reportException($ex);

                if ($use_transaction)
                    self::getDBTable()->getAdapter()->rollback();

                return false;
            };

        }


        public static function checkUniqueLogin($values)
        {
            try
            {
                $select = self::getDBTable()->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->where("login=?", $values['login'])->where("company_id=?", $values['company_id'])->where("id<>?", $values['id'])->columns("id");
                return (is_null(self::getDBTable()->fetchRow($select)));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function save(Principio_Model_Company_Abstract $model_company_abstract)
        {
            try
            {
                $data = [
                    'caption' => $model_company_abstract->getCaption(),
                    'proxy' => $model_company_abstract->getProxy(),
                    'global_template' => $model_company_abstract->getGlobalTemplate(),
                    'ext_template' => $model_company_abstract->getExtTemplate(),
                    'vlan' => $model_company_abstract->getVlanID(),
                    'vlan_pc' => $model_company_abstract->getVlanPcID(),
                    'default_gw' => $model_company_abstract->getDefaultGw(),
                    'netname' => $model_company_abstract->getNetname(),
                    'netmask' => $model_company_abstract->getNetmask(),
                    'broadcast' => $model_company_abstract->getBroadcast(),
                    'range_from' => $model_company_abstract->getRangeFrom(),
                    'range_to' => $model_company_abstract->getRangeTo(),
                    'ip' => $model_company_abstract->getIp(),
                    'pool_id' => -1,
                    'type' => $model_company_abstract->getType()
                ];

                if ($model_company_abstract->getID() == -1)
                    $model_company_abstract->setID(self::getDBTable()->insert($data));
                else
                    self::getDBTable()->update($data, ['id=?' => $model_company_abstract->getID()]);

                return true;
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function fetch($id)
        {
            try
            {
                $company_raw = self::getDBTable()->find(intval($id));
                return (is_null($company_raw) ? null : Principio_Model_Company_Abstract::fromArray($company_raw[0]->toArray()));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        protected static function fetchByType($type, $except_model_company_abstract = null, PrincipioLab_IComparator $IComparator = null)
        {
            try
            {
                $select = self::getDBTable()->select()->order("caption DESC")->where("type=?", $type);

                if (!is_null($except_model_company_abstract))
                    $select = $select->where("id<>?", $except_model_company_abstract->getID());

                $companies_raw = self::getDBTable()->fetchAll($select);

                return Principio_Model_Company_Abstract::makeCollection($companies_raw->toArray(), $IComparator);
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }



        public static function fetchAll($except_model_company_abstract = null, PrincipioLab_IComparator $IComparator = null)
        {
            return Principio_Model_Company_AbstractMapper::fetchByType(Principio_Model_Company::TYPE_COMPANY, $except_model_company_abstract, $IComparator);
        }


        public static function getRoot($except_model_company_abstract = null)
        {
            $comparator_companies_root = new Principio_Comparator_CompaniesRoot();

            return Principio_Model_PoolMapper::fetchAll($except_model_company_abstract, $comparator_companies_root)->appendList(Principio_Model_CompanyMapper::fetchAll($except_model_company_abstract, $comparator_companies_root));
        }


        public static function getNotPool()
        {
            return Principio_Model_CompanyInPoolMapper::fetchAll()->appendList(Principio_Model_CompanyMapper::fetchAll());
        }


        public static function checkUniqueCaption(Principio_Model_Company_Abstract $model_company_abstract)
        {
            try
            {
                $select = self::getDBTable()->select("id")->where("caption=?", $model_company_abstract->getCaption())->where("id<>?", $model_company_abstract->getID());
                return (count(self::getDBTable()->fetchAll($select)) == 0);
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function updateTemplates(Principio_Model_Company_Abstract $model_company_abstract)
        {
            try
            {
                $data = (['global_template' => $model_company_abstract->getGlobalTemplate(), 'ext_template' => $model_company_abstract->getExtTemplate()]);
                self::getDBTable()->update($data, ['id=?' => $model_company_abstract->getID()]);

                return true;
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        /*
        public static function search($string)
        {
            $search_fields = array(
                "caption" => "Caption",
                "proxy" => "Proxy",
                "global_template" => "Global Template",
                "ext_template" => "Ext template",
                "vlan" => "Vlan",
                "vlan_pc" => "Vlan pc",
                "default_gw" => "Default GW",
                "netname" => "Netname",
                "netmask" => "Netmask",
                "broadcast" => "Broadcast",
                "range_from" => "Range from",
                "range_to" => "Range to",
                "pool_id" => "pool id",
                "type" => "Type"
            );
            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);
            foreach ($result as $res)
            {
                $model_search = new Principio_Model_Search($string, "Companies");
                $menu_data = array("menu_name" => "view_by_company", "level" => EXPAND_VIEW_BY_COMPANY, "open_level" => 1);
                $model_search->setId($res["id"])->setResultName($res["caption"])->setOpenItemFunctionName("ViewByCompany")->setMenuData($menu_data);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                        $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["id"]] = $model_search;
            }
            return $search;
        }
        */
    }
?>